package com.coleloca.androidlongs.collectlocationplication;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.coleloca.androidlongs.collectlocationplication.http.HttpUrlHelper;
import com.studyyoun.android.androidcommonbaselibrary.activity.CommonBaseActivity;
import com.studyyoun.android.androidcommonbaselibrary.okhttp.callback.OkhttpCommonCallBack;
import com.studyyoun.android.androidcommonbaselibrary.okhttp.client.OkhttpCommonClient;
import com.studyyoun.android.androidcommonbaselibrary.utils.CommonLoadingUtils;
import com.studyyoun.android.androidcommonbaselibrary.utils.LogUtils;
import com.studyyoun.android.androidcommonbaselibrary.utils.ToastUtils;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

public class MainActivity extends CommonBaseActivity {

    private TextView mUpdateSchoolTextView;
    private RecyclerView mSchoolRecyclerView;

    @Override
    protected void getAllIntentExtraDatas(Intent intent) {

    }

    @Override
    protected int getCommonLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void commonInitView(View view) {

        mUpdateSchoolTextView = findViewById(R.id.tv_update_school_list);
        mSchoolRecyclerView = findViewById(R.id.rv_school_list);
    }

    @Override
    protected void commonFunction() {

    }

    @Override
    protected void commonDelayFunction() {
        mUpdateSchoolTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View lView) {
                getSchoolListFunction();
            }
        });
    }

    private void getSchoolListFunction() {
        String addUrl = HttpUrlHelper.getSchoolListUrl;

        ConcurrentHashMap<String, String> kepMap = new ConcurrentHashMap<>();


        kepMap.put("app", "1");

        CommonLoadingUtils.getInstance().showLoading(mUpdateSchoolTextView);

        OkhttpCommonClient.sentGetRequest(addUrl, kepMap, new OkhttpCommonCallBack(SchoolModel.class) {
            @Override
            public void onSuccess(Object obj) {
                LogUtils.d("request success ");
                CommonLoadingUtils.getInstance().closeFunction();

                List<SchoolModel> schoolModelList = (List<SchoolModel>) obj;

                updateSchoolListFucniton(schoolModelList);

            }

            @Override
            public void onFaile(Object obj) {
                ToastUtils.show("获取失败，网络异常", mContext);
                CommonLoadingUtils.getInstance().closeFunction();
            }
        });
    }

    private SchoolListAdapter mSchoolListAdapter;

    private void updateSchoolListFucniton(List<SchoolModel> lSchoolModelList) {

        if (mSchoolListAdapter == null) {
            mSchoolListAdapter = new SchoolListAdapter(lSchoolModelList);
            mSchoolRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
            mSchoolRecyclerView.setAdapter(mSchoolListAdapter);
        } else {
            mSchoolListAdapter.updateListDate(lSchoolModelList);
            mSchoolListAdapter.notifyDataSetChanged();
        }
    }


    public class SchoolListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        public List<SchoolModel> mSchoolModelList;

        public SchoolListAdapter(List<SchoolModel> lSchoolModelList) {
            mSchoolModelList = lSchoolModelList;
        }

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new SchoolViewHolder(View.inflate(mContext,R.layout.item_school_infor,null));
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            SchoolModel schoolModel = mSchoolModelList.get(position);
            SchoolViewHolder schoolViewHolder = (SchoolViewHolder) holder;
            schoolViewHolder.setDatas(schoolModel,position);
        }

        @Override
        public int getItemCount() {
            return mSchoolModelList == null ? 0 : mSchoolModelList.size();
        }

        public void updateListDate(List<SchoolModel> lSchoolModelList) {
            mSchoolModelList = lSchoolModelList;
        }
    }

    public class SchoolViewHolder extends RecyclerView.ViewHolder{

        private final TextView mSchoolNameTextView;

        public SchoolViewHolder(View itemView) {
            super(itemView);
            mSchoolNameTextView = itemView.findViewById(R.id.tv_school_name);
        }

        public void setDatas(final SchoolModel lSchoolModel, int lPosition) {
            mSchoolNameTextView.setText(""+lSchoolModel.getName());

            mSchoolNameTextView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View lView) {
                    Intent intent = new Intent(mContext, HomeActivity.class);
                    intent.putExtra("schoolMolde",lSchoolModel);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    mContext.startActivity(intent);
                }
            });
        }
    }
}
